# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >

_realname=matplotlib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.5.1
pkgrel=1
pkgdesc="A python plotting library, making publication quality plots (mingw-w64)"
arch=('any')
url='http://matplotlib.org'
license=('custom')
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python2-pytz"
            "${MINGW_PACKAGE_PREFIX}-python3-pytz"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy"
            "${MINGW_PACKAGE_PREFIX}-python3-numpy"
            "${MINGW_PACKAGE_PREFIX}-python2-cairo"
            "${MINGW_PACKAGE_PREFIX}-python3-cairo"
            "${MINGW_PACKAGE_PREFIX}-python2-pyqt5"
            "${MINGW_PACKAGE_PREFIX}-python3-pyqt5"
            "${MINGW_PACKAGE_PREFIX}-python2-dateutil"
            "${MINGW_PACKAGE_PREFIX}-python3-dateutil"
            "${MINGW_PACKAGE_PREFIX}-python2-pyparsing"
            "${MINGW_PACKAGE_PREFIX}-python3-pyparsing"
            "${MINGW_PACKAGE_PREFIX}-python2-cycler"
            "${MINGW_PACKAGE_PREFIX}-python3-cycler"
            "${MINGW_PACKAGE_PREFIX}-freetype"
            "${MINGW_PACKAGE_PREFIX}-libpng"
            #"${MINGW_PACKAGE_PREFIX}-qhull"
)
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/matplotlib/matplotlib/archive/v${pkgver}.tar.gz"
        setupext.py.patch
        setup.py.patch)
sha256sums=('64c9f7cce4075d05c2602108ee1795aa6455c2c1d8e4329c95c83465e3806887'
            'ecaa1039d7527e31bbf175b23802123e843678bf0109fee4c76742d8eeee1fe0'
            '17e4353062e165dfe54293e39d8924355a4a3c44f2ffee3739d7cc7bddc9c699')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  # Clean up old sources so re-patching doesn't fail.
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf $srcdir/${_realname}-${pkgver}
  tar -xzvf ${_realname}-${pkgver}.tar.gz -C ${srcdir}

  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/setupext.py.patch
  patch -p1 -i ${srcdir}/setup.py.patch
  cd ${srcdir}

  cp -a ${_realname}-${pkgver} ${_realname}-py2-${pkgver}
  cp -a ${_realname}-${pkgver} ${_realname}-py3-${pkgver}

  cd ${srcdir}/${_realname}-py3-${pkgver}
  # remove cairo which fails for python3
  sed -i 's/setupext\.BackendGtk3Cairo/#setupext\.BackendGtk3Cairo/g' setup.py
  sed -i 's/setupext\.BackendCairo/#setupext\.BackendCairo/g' setup.py

  cd ${srcdir}/${_realname}-py2-${pkgver}
  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/env python2|" \
    -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
    -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
    -i $(find . -name '*.py')
}

build() {
  cd ${srcdir}/${_realname}-py2-${pkgver}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py build

  cd ${srcdir}/${_realname}-py3-${pkgver}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package_python3-matplotlib() {
    depends=(
        "${MINGW_PACKAGE_PREFIX}-python3-pytz"
        "${MINGW_PACKAGE_PREFIX}-python3-numpy"
        "${MINGW_PACKAGE_PREFIX}-python3-cairo"
        "${MINGW_PACKAGE_PREFIX}-python3-cycler"
        "${MINGW_PACKAGE_PREFIX}-python3-pyqt5"
        "${MINGW_PACKAGE_PREFIX}-python3-dateutil"
        "${MINGW_PACKAGE_PREFIX}-python3-pyparsing"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        #"${MINGW_PACKAGE_PREFIX}-qhull"
    )
   cd ${srcdir}/${_realname}-py3-${pkgver}

   MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python3 setup.py install -O1 --skip-build \
   --root="${pkgdir}" --prefix=${MINGW_PREFIX}

   install -Dm644 doc/users/license.rst "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-matplotlib/LICENSE
}

package_python2-matplotlib() {
    depends=(
        "${MINGW_PACKAGE_PREFIX}-python2-pytz"
        "${MINGW_PACKAGE_PREFIX}-python2-numpy"
        "${MINGW_PACKAGE_PREFIX}-python2-cairo"
        "${MINGW_PACKAGE_PREFIX}-python2-cycler"
        "${MINGW_PACKAGE_PREFIX}-python2-pyqt5"
        "${MINGW_PACKAGE_PREFIX}-python2-dateutil"
        "${MINGW_PACKAGE_PREFIX}-python2-pyparsing"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        #"${MINGW_PACKAGE_PREFIX}-qhull"
    )
   cd ${srcdir}/${_realname}-py2-${pkgver}

   MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python2 setup.py install -O1 --skip-build \
   --root="${pkgdir}" --prefix=${MINGW_PREFIX}

   install -Dm644 doc/users/license.rst "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-matplotlib/LICENSE
}

package_mingw-w64-i686-python2-matplotlib() {
  package_python2-matplotlib
}

package_mingw-w64-i686-python3-matplotlib() {
  package_python3-matplotlib
}

package_mingw-w64-x86_64-python2-matplotlib() {
  package_python2-matplotlib
}

package_mingw-w64-x86_64-python3-matplotlib() {
  package_python3-matplotlib
}
